//
//  ViewDBPage.m
//  MyBarcodeStore
//
//  Created by Mac on 2010/5/17.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ViewDBPage.h"
#import "MyStoreParsedResult.h"
#import "Utils/THUIImageWithString.h"

@implementation ViewDBPage

- (id)init 
{
	if (self = [super init]) 
	{
		self.title = @"瀏覽資料庫";
		self.navigationItem.title = @"瀏覽資料庫";
		myDB = [[DbObject alloc] init];
		myData = [[myDB getItems] retain];
	}
	return self;
}


/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/

- (void)viewDidLoad {
    [super viewDidLoad];
#ifdef DEBUG
	NSLog(@"Init view.");
#endif

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
	
	//myData = [myDB getItems];
	
	// Add button for Create email lanucher
	/*
	UIBarButtonItem *EmailSender = [[UIBarButtonItem alloc] 
							   initWithTitle:NSLocalizedString(@"Email", @"") style:UIBarButtonItemStyleBordered 
							   target:self action:@selector(mailSend:)];
	
	self.navigationItem.rightBarButtonItem = EmailSender;
	*/
}

/**
 * All about Mail
 */
/*
- (void) mailSend:(id)sender 
{
#ifdef DEBUG
	NSLog(@"Create a new mail to send with attached files.");
#endif
	MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
	picker.mailComposeDelegate = self;
	
	[picker setSubject:@"Hello!"];
	
	//[self presentModalViewController:picker animated:YES];
	[picker release];
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
#ifdef DEBUG
	NSLog(@"MailComposer did finished.");
#endif
	[self dismissModalViewControllerAnimated:YES];
}
*/

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
#ifdef DEBUG
	NSLog(@"ViewDB page will show.");
#endif
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [myData count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    // Set up the cell...
	MyStoreParsedResult *result = (MyStoreParsedResult *)[(NSMutableDictionary *)[myData objectAtIndex:indexPath.row] objectForKey:@"barcode"];
	cell.textLabel.text = result.name;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
	
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

- (MyStoreParsedResult *)resultAtIndex:(NSUInteger)index {
	if (myData != nil) {
		return (MyStoreParsedResult *)[(NSMutableDictionary *)[myData objectAtIndex:index] objectForKey:@"barcode"];
	}
	return nil;
}

- (void)dealloc {
	[myDB release];
	[myData release];
    [super dealloc];
}

@end

